package org.jivesoftware.smack.util;

import java.util.Map;

public class DNSUtil
{
  private static Map<String, HostAddress> ccache = new Cache(100, 600000L);
  private static Map<String, HostAddress> scache = new Cache(100, 600000L);

  // ERROR //
  private static HostAddress resolveSRV(String paramString)
  {
    // Byte code:
    //   0: new 35	org/xbill/DNS/Lookup
    //   3: dup
    //   4: aload_0
    //   5: bipush 33
    //   7: invokespecial 38	org/xbill/DNS/Lookup:<init>	(Ljava/lang/String;I)V
    //   10: invokevirtual 42	org/xbill/DNS/Lookup:run	()[Lorg/xbill/DNS/Record;
    //   13: astore 6
    //   15: aload 6
    //   17: ifnonnull +9 -> 26
    //   20: aconst_null
    //   21: astore 4
    //   23: goto +236 -> 259
    //   26: aload 6
    //   28: arraylength
    //   29: istore 7
    //   31: iconst_0
    //   32: istore 8
    //   34: aconst_null
    //   35: astore_2
    //   36: bipush 255
    //   38: istore_3
    //   39: ldc 43
    //   41: istore 9
    //   43: iconst_0
    //   44: istore 10
    //   46: iload 8
    //   48: iload 7
    //   50: if_icmplt +13 -> 63
    //   53: aload_2
    //   54: ifnonnull +140 -> 194
    //   57: aconst_null
    //   58: astore 4
    //   60: goto +199 -> 259
    //   63: aload 6
    //   65: iload 8
    //   67: aaload
    //   68: checkcast 45	org/xbill/DNS/SRVRecord
    //   71: astore 13
    //   73: aload 13
    //   75: ifnull +187 -> 262
    //   78: aload 13
    //   80: invokevirtual 49	org/xbill/DNS/SRVRecord:getTarget	()Lorg/xbill/DNS/Name;
    //   83: ifnull +179 -> 262
    //   86: aload 13
    //   88: invokevirtual 53	org/xbill/DNS/SRVRecord:getWeight	()I
    //   91: aload 13
    //   93: invokevirtual 53	org/xbill/DNS/SRVRecord:getWeight	()I
    //   96: imul
    //   97: i2d
    //   98: invokestatic 59	java/lang/Math:random	()D
    //   101: dmul
    //   102: d2i
    //   103: istore 14
    //   105: aload 13
    //   107: invokevirtual 62	org/xbill/DNS/SRVRecord:getPriority	()I
    //   110: iload 9
    //   112: if_icmpge +32 -> 144
    //   115: aload 13
    //   117: invokevirtual 62	org/xbill/DNS/SRVRecord:getPriority	()I
    //   120: istore 9
    //   122: iload 14
    //   124: istore 10
    //   126: aload 13
    //   128: invokevirtual 49	org/xbill/DNS/SRVRecord:getTarget	()Lorg/xbill/DNS/Name;
    //   131: invokevirtual 68	org/xbill/DNS/Name:toString	()Ljava/lang/String;
    //   134: astore_2
    //   135: aload 13
    //   137: invokevirtual 71	org/xbill/DNS/SRVRecord:getPort	()I
    //   140: istore_3
    //   141: goto +121 -> 262
    //   144: aload 13
    //   146: invokevirtual 62	org/xbill/DNS/SRVRecord:getPriority	()I
    //   149: iload 9
    //   151: if_icmpne +111 -> 262
    //   154: iload 14
    //   156: iload 10
    //   158: if_icmple +104 -> 262
    //   161: aload 13
    //   163: invokevirtual 62	org/xbill/DNS/SRVRecord:getPriority	()I
    //   166: istore 9
    //   168: iload 14
    //   170: istore 10
    //   172: aload 13
    //   174: invokevirtual 49	org/xbill/DNS/SRVRecord:getTarget	()Lorg/xbill/DNS/Name;
    //   177: invokevirtual 68	org/xbill/DNS/Name:toString	()Ljava/lang/String;
    //   180: astore_2
    //   181: aload 13
    //   183: invokevirtual 71	org/xbill/DNS/SRVRecord:getPort	()I
    //   186: istore 15
    //   188: iload 15
    //   190: istore_3
    //   191: goto +71 -> 262
    //   194: aload_2
    //   195: ldc 73
    //   197: invokevirtual 79	java/lang/String:endsWith	(Ljava/lang/String;)Z
    //   200: ifeq +15 -> 215
    //   203: aload_2
    //   204: iconst_0
    //   205: aload_2
    //   206: invokevirtual 82	java/lang/String:length	()I
    //   209: iconst_1
    //   210: isub
    //   211: invokevirtual 86	java/lang/String:substring	(II)Ljava/lang/String;
    //   214: astore_2
    //   215: new 6	org/jivesoftware/smack/util/DNSUtil$HostAddress
    //   218: dup
    //   219: aload_2
    //   220: iload_3
    //   221: aconst_null
    //   222: invokespecial 89	org/jivesoftware/smack/util/DNSUtil$HostAddress:<init>	(Ljava/lang/String;ILorg/jivesoftware/smack/util/DNSUtil$HostAddress;)V
    //   225: astore 4
    //   227: goto +32 -> 259
    //   230: astore 5
    //   232: aconst_null
    //   233: astore_2
    //   234: bipush 255
    //   236: istore_3
    //   237: goto -184 -> 53
    //   240: astore 12
    //   242: goto -189 -> 53
    //   245: astore_1
    //   246: aconst_null
    //   247: astore_2
    //   248: bipush 255
    //   250: istore_3
    //   251: goto -198 -> 53
    //   254: astore 11
    //   256: goto -203 -> 53
    //   259: aload 4
    //   261: areturn
    //   262: iinc 8 1
    //   265: goto -219 -> 46
    //
    // Exception table:
    //   from	to	target	type
    //   0	31	230	java/lang/NullPointerException
    //   63	188	240	java/lang/NullPointerException
    //   0	31	245	org/xbill/DNS/TextParseException
    //   63	188	254	org/xbill/DNS/TextParseException
  }

  public static HostAddress resolveXMPPDomain(String paramString)
  {
    HostAddress localHostAddress1;
    Object localObject3;
    ??? = ccache;
    monitorenter;
    try
    {
      if (ccache.containsKey(paramString))
      {
        HostAddress localHostAddress2 = (HostAddress)ccache.get(paramString);
        if (localHostAddress2 != null)
        {
          monitorexit;
          localObject3 = localHostAddress2;
        }
      }
      else
      {
        monitorexit;
        localHostAddress1 = resolveSRV("_xmpp-client._tcp." + paramString);
        if (localHostAddress1 == null)
          localHostAddress1 = resolveSRV("_jabber._tcp." + paramString);
        localHostAddress1 = new HostAddress(paramString, 5222, null);
      }
    }
    finally
    {
      Map localMap2;
      synchronized (ccache)
      {
        ccache.put(paramString, localHostAddress1);
        monitorexit;
        localObject3 = localHostAddress1;
        break label150:
        localObject1 = finally;
        throw localObject1;
      }
    }
    label150: return ((HostAddress)localObject3);
  }

  public static HostAddress resolveXMPPServerDomain(String paramString)
  {
    HostAddress localHostAddress1;
    Object localObject3;
    ??? = scache;
    monitorenter;
    try
    {
      if (scache.containsKey(paramString))
      {
        HostAddress localHostAddress2 = (HostAddress)scache.get(paramString);
        if (localHostAddress2 != null)
        {
          monitorexit;
          localObject3 = localHostAddress2;
        }
      }
      else
      {
        monitorexit;
        localHostAddress1 = resolveSRV("_xmpp-server._tcp." + paramString);
        if (localHostAddress1 == null)
          localHostAddress1 = resolveSRV("_jabber._tcp." + paramString);
        localHostAddress1 = new HostAddress(paramString, 5269, null);
      }
    }
    finally
    {
      Map localMap2;
      synchronized (scache)
      {
        scache.put(paramString, localHostAddress1);
        monitorexit;
        localObject3 = localHostAddress1;
        break label150:
        localObject1 = finally;
        throw localObject1;
      }
    }
    label150: return ((HostAddress)localObject3);
  }

  public static class HostAddress
  {
    private String host;
    private int port;

    private HostAddress(String paramString, int paramInt)
    {
      this.host = paramString;
      this.port = paramInt;
    }

    public boolean equals(Object paramObject)
    {
      int i;
      if (this == paramObject)
        i = 1;
      while (true)
      {
        while (true)
        {
          HostAddress localHostAddress;
          while (true)
          {
            while (true)
            {
              return i;
              if (paramObject instanceof HostAddress)
                break;
              i = 0;
            }
            localHostAddress = (HostAddress)paramObject;
            if (this.host.equals(localHostAddress.host))
              break;
            i = 0;
          }
          if (this.port != localHostAddress.port)
            break;
          i = 1;
        }
        i = 0;
      }
    }

    public String getHost()
    {
      return this.host;
    }

    public int getPort()
    {
      return this.port;
    }

    public String toString()
    {
      return this.host + ":" + this.port;
    }
  }
}